package de.tu_darmstadt.gdi1.battleship.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.MessageFormat;

import de.tu_darmstadt.gdi1.battleship.exceptions.FieldAlreadyShotException;
import de.tu_darmstadt.gdi1.battleship.exceptions.InternalFailureException;
import de.tu_darmstadt.gdi1.battleship.exceptions.InvalidCoordinatesException;
import de.tu_darmstadt.gdi1.battleship.exceptions.InvalidLevelFileException;
import de.tu_darmstadt.gdi1.battleship.exceptions.NotUsersTurnException;
import de.tu_darmstadt.gdi1.battleship.model.BattleshipGame;
import de.tu_darmstadt.gdi1.battleship.model.Coordinates;
import de.tu_darmstadt.gdi1.battleship.net.BattleshipClient;
import de.tu_darmstadt.gdi1.battleship.net.NetObject;
import de.tu_darmstadt.gdi1.battleship.view.Messages;
import de.tu_darmstadt.gdi1.battleship.view.StudentWindow;

public class NetworkClientInteraction  extends UserInteraction{
	
	BattleshipClient netClient = new BattleshipClient();

	public NetworkClientInteraction(BattleshipGame battleshipGame,
			StudentWindow wnd, String ip) {
		super(battleshipGame, wnd);
		
		try {
			netClient.init(InetAddress.getByName(ip), 1337);
			 NetObject no = netClient.receiveObject();
				bsg.readLvlFromString(no.getLvlString());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (InvalidLevelFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		bsg.setTurn(false);
		
		
	}

	
	
	public void userClicked(int posX, int posY) {

		if(bsg.getTurn()){
				playerShotClick(posX, posY);
		}
	
	
//			
//			new Thread() {
//
//				public void run() {
					
					NetObject no = netClient.receiveObject();
					System.out.println("recieved turn:" + no.getTurn());
					// Receive update from server
						try {						
							bsg.readLvlFromString(no.getLvlString());
							mainWnd.getGamePanel().redraw();
							no.setCorrupt(false);
						} catch (Exception e){
							if (no==null) no = netClient.receiveObject();
							no.setCorrupt(true);
							netClient.sendObject(no);
						}
					
					bsg.setTurn(!no.getTurn());
				
					
					try {
						mainWnd.getGamePanel().redraw();
					} catch (InternalFailureException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					// If game has ended notify Player and stop.
					if (bsg.isFinished() && bsg.getWinner() == 2) {
						Object[] args = { bsg.getPlayerName(),
								new Integer(bsg.getShotsUsed()) };
						MessageFormat formatter = new MessageFormat("");
						formatter.applyPattern(Messages.getString("Info.lost"));
						mainWnd.showMessage(formatter.format(args));

						return;
					}
//				}
//			}.start();
	}
	
		
	
	public void playerShotClick(int x, int y) {
		// to work only in enemy Map, shift X to the right.
		x -= 11;
		
		Coordinates target;
		try {
			target = new Coordinates(x,y);
		} catch (InvalidCoordinatesException e1) {
			return;
		}
		
		NetObject no = new NetObject();
		no.setMessage("Shoot at " + x + ", " + y);
		no.setPos(10*x+y);
		netClient.sendObject(no);
		
		try {// player shoots
			bsg.playerShoot(target);
		} catch (NotUsersTurnException e) {
			mainWnd.showMessage(Messages.getString("Info.notUsersTurn"));
		} catch (InvalidCoordinatesException e) {
			mainWnd.showMessage(Messages.getString("Info.invCoords"));
		} catch (FieldAlreadyShotException e) {
			mainWnd.showMessage(Messages.getString("Info.isShot"));
		}

		// renew output
		try {
			mainWnd.getGamePanel().redraw();
		} catch (InternalFailureException e) {
			System.err.println(Messages.getString("Info.redrawErr"));
		}

	}

	}
